<?php
/**
 * #Celebrate Shieh
 *             ,%%%%%%%%,
 *           ,%%/\%%%%/\%%
 *          ,%%%\c "" J/%%%
 * %.       %%%%/ o  o \%%%
 * `%%.     %%%%    _  |%%%
 *  `%%     `%%%%(__Y__)%%'
 *  //       ;%%%%`\-/%%%'
 * ((       /  `%%%%%%%'
 *  \\    .'          |
 *   \\  /       \  | |
 *    \\/         ) | |
 *     \         /_ | |__
 *     (___________))))))) 攻城湿
 *
 *        _       _
 * __   _(_)_   _(_) __ _ _ __
 * \ \ / / \ \ / / |/ _` |'_ \
 *  \ V /| |\ V /| | (_| | | | |
 *   \_/ |_| \_/ |_|\__,_|_| |_|
 */

namespace App\Services;

use App\Models\Common\FailedJob;
use Illuminate\Http\Request;

class JobService
{

    use Service;

    /**
     * 邮件队列
     *
     * @param Request $request
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function JobIndexService( $request )
    {
        if ( ! $request->filled('static') ) request()->offsetSet('static', -1);
        $where  = function ( $query ) use ( $request ) {
            if ( $request->filled('status') and $request->input('status') != '-1' ) $query->where('status', $request->input('status'));
            if ( $request->filled('static') and $request->input('static') != '-1' ) $query->where('static', $request->input('static'));
        };
        $select = [ 'id', 'key', 'content', 'updated_at', 'count', 'static', 'status' ];
        $job    = FailedJob::GetModel($where, [], $select);
        foreach ( $job as &$value ) $value->content = json_decode($value->content, TRUE);
        $job->appends([
            'status' => $request->input('status'),
            'static' => $request->input('static'),
        ])->render();//添加到request
        return view('expansion.job.index', compact('job', 'request'));
    }


    /**
     * 删除
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
     */
    public function JobDeleteService( $request )
    {
        FailedJob::DestroyModel($request->input('id'));
        if ( $request->ajax() ) return data(NULL, $this->getErrorCode()::message(204));
        else return back()->with('success', $this->getErrorCode()::message(204));
    }

    /**
     * 修改队列
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function JobStatusService( $request )
    {
        try {
            $where  = function ( $query ) use ( $request ) {
                $query->where('id', $request->input('id'));
            };
            $select = [ 'id', 'key', 'content', 'count', 'static', 'status', 'updated_at' ];
            $job    = FailedJob::FirstModel($where, [], $select);
            switch ( $request->input('status') ) {
                case 'hide':
                    $job->status = $job->status == 1 ? 2 : 1;
                    $job->save();
                    break;
                case 'check':
                    $failure = emailMail(json_decode($job->content, TRUE));
                    if ( ! $failure ) {
                        $data[ 'count' ]  = $job->count + 1;
                        $data[ 'static' ] = 2;
                        $job->update($data);
                    }
                    else {
                        $data[ 'static' ] = 1;
                        $job->update($data);
                    }
                    break;
                case 'put':
                    $job->static = 0;
                    $job->count  = 0;
                    $job->save();
                    break;
            }
        } catch ( \Exception $exception ) {
            $data[ 'count' ]  = $job->count + 1;
            $data[ 'static' ] = 2;
            $job->update($data);
            errorStorage('job', $exception->getTraceAsString(), $exception->getMessage(), 'error', '修改队列');
            return data(NULL, $this->getErrorCode()::message(1000), 1000);
        }
        return data(NULL, $request->input('status') == 'hide' ? $this->getErrorCode()::message(210) :
            $request->input('status') == 'put' ? $this->getErrorCode()::message(212) : $this->getErrorCode()::message(211));
    }

}
